package licode.unisop.platform.client.authinfo;

import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class AuUserProps {
    private int currVersion;
    private int loadVersion;

    private Map<String, AuPlatformProp> plProps;

    public AuUserProps() {
        currVersion = 0;
        loadVersion = 0;
        plProps = new HashMap<>();
    }

    public void updateVersion() {
        currVersion += 1;
        loadVersion = currVersion;
    }

    public void updateLoadVersion() {
        loadVersion += 1;
    }

    public boolean isUpdated() {
        return loadVersion != currVersion;
    }

    public AuPlatformProp getPlatformProp(String platformId) {
        return plProps.get(platformId);
    }

    public List<String> getPlatformUserMenu(String platformId) {
        AuPlatformProp plProp = plProps.get(platformId);
        if (null != plProp && plProp.getPlProp() != null) {
            return plProp.getPlProp().getForbidMenus();
        }
        return null;
    }

    public AuProperties findProp(String platformId, String clientId) {
        AuProperties auProp;
        AuPlatformProp plProp = getPlatformProp(platformId);
        if (null == plProp) {
            return null;
        }
        auProp = plProp.findProp(clientId);
        if (null != auProp && auProp.isEmpty()) {
            auProp = plProp.getPlProp();
            if (auProp != null && auProp.isEmpty()) {
                auProp = null;
            }
        }
        return auProp;
    }
}
